package org.file5.controller.gaefile;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.file5.domain.Attachment;
import org.file5.service.ServiceFascade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class GaefileController {
	private final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private ServiceFascade sf;

	/**
	 * Stream SwagImage.image from database or show defaultImage if there is
	 * none
	 */
	@RequestMapping(value = "/gaefile/{uid}.{fileExt}", method = RequestMethod.GET)
	public void streamFileContent(@PathVariable("uid") String uid,
			@PathVariable("fileExt") String fileExt, HttpServletRequest req,
			HttpServletResponse response, OutputStream outputStream)
			throws IOException {
		// set to no cache: NOTE this could fix caching problem
		// but may not be ideal to stop all browser image caching
		// response.setDateHeader( "Expires", DateUtils.addMonths( new Date(),
		// -2 ).getTime() );
		Attachment attachment = sf.getBaseService().find(Attachment.class, uid);
		byte[] contentBytes = null;
		// if there's no image, return default image
		if (attachment.getFileContent() != null) {
			contentBytes = attachment.getFileContent().getBytes();
		}
		if (contentBytes != null) {
			try {
				outputStream.write(contentBytes, 0, contentBytes.length);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

	/**
	 * Stream SwagImage.image from database or show defaultImage if there is
	 * none
	 */
	@RequestMapping(value = "/gaeimage/{uid}.{fileExt}", method = RequestMethod.GET)
	public void streamImageContent(@PathVariable("uid") String uid,
			@PathVariable("fileExt") String fileExt, HttpServletRequest req,
			HttpServletResponse response, OutputStream outputStream)
			throws IOException {
		// set to no cache: NOTE this could fix caching problem
		// but may not be ideal to stop all browser image caching
		// response.setDateHeader( "Expires", DateUtils.addMonths( new Date(),
		// -2 ).getTime() );
		Attachment swagImage = sf.getBaseService().find(Attachment.class, uid);
		byte[] swagImageBytes;
		// if there's no image, return default image
		if (swagImage.getFileContent() == null) {
			swagImageBytes = sf.getImageService().getDefaultImageBytes(
					req.getRequestURL().toString());
		} else {
			swagImageBytes = swagImage.getFileContent().getBytes();
		}
		try {
			outputStream.write(swagImageBytes, 0, swagImageBytes.length);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Stream SwagImage.image from database or show defaultImage if there is
	 * none
	 */
	@RequestMapping(value = "/gaeimage/thumbnail/{width}/{height}/{uid}.{fileExt}", method = RequestMethod.GET)
	public void streamThumbnailContent(@PathVariable("width") Integer width,
			@PathVariable("height") Integer height,
			@PathVariable("uid") String uid,
			@PathVariable("fileExt") String fileExt, HttpServletRequest req,
			OutputStream outputStream) throws IOException {

		byte[] swagImageBytes = sf.getAttachmentService().getThumbnailBytes(
				uid, width, height);
		// if there's no image, return default image
		if (swagImageBytes == null) {
			swagImageBytes = sf.getImageService()
					.getResizedThumbnailImageBytes(
							width,
							height,
							sf.getImageService().getDefaultImageBytes(
									req.getRequestURL().toString()));
		}

		try {
			outputStream.write(swagImageBytes, 0, swagImageBytes.length);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}